﻿using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLineNew.Core.Extensions;
using ZoneLineNew.Enums;
using ZoneLineNew.Models.Dto;
using ZoneLineNew.Models.Entity.BaseInfo;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.ViewModelBase;

namespace ZoneLineNew.ViewModels.Dialogs {
    public class EditProductDialogViewModel : DialogViewModel {

        public EditProductDialogViewModel(
            IRegionManager regionManager, 
            IDialogService dialog, 
            IEventAggregator eventAggregator, 
            IMessageService message, 
            IDbOperate<IFreeSqlMainContext> mainDbOperate
            ) : base(regionManager, dialog, eventAggregator, message, mainDbOperate) {

            ProductEntity = new ProductEntity();

            var elist = EnumExtensions.ConvertToComboxItems<WorkDirectionEnum>();
            WorkDirections.AddRange(elist);
        }

        private ObservableCollection<CustomerEntity> customers = new ObservableCollection<CustomerEntity>();

        public ObservableCollection<CustomerEntity> Customers {
            get { return customers; }
            set { customers = value; base.RaisePropertyChanged(); }
        }

        private ObservableCollection<ComboxItemDto> workDirections = new ObservableCollection<ComboxItemDto>();

        public ObservableCollection<ComboxItemDto> WorkDirections {
            get { return workDirections; }
            set { workDirections = value; base.RaisePropertyChanged(); }
        }

        private ProductEntity productEntity;


        public ProductEntity ProductEntity {
            get { return productEntity; }
            set { productEntity = value; base.RaisePropertyChanged(); }
        }


        public DelegateCommand SaveCommand => new DelegateCommand(() => {
            int affected = 0;

            if (ProductEntity.CustomerID == 0) {
                _message.Warning("请选择客户！");
                return;
            }

            if (string.IsNullOrEmpty(ProductEntity.ProductName.ToString())) {
                _message.Warning("产品名称必填！");
                return;
            }

            if (string.IsNullOrEmpty(ProductEntity.Description.ToString())) {
                _message.Warning("产品描述必填！");
                return;
            }

            if (string.IsNullOrEmpty(ProductEntity.Model.ToString())) {
                _message.Warning("产品型号必填！");
                return;
            }

            if (ProductEntity.WorkDirection == WorkDirectionEnum.None) {
                _message.Warning("产品方向必填！");
                return;
            }

            if (string.IsNullOrEmpty(ProductEntity.MaterialNumber.ToString())) {
                _message.Warning("物料编号必填！");
                return;
            }

            if (string.IsNullOrEmpty(ProductEntity.Unit.ToString())) {
                _message.Warning("单位必填！");
                return;
            }
           

            affected = _mainDbOperate.GetFreeSql().InsertOrUpdate<ProductEntity>().SetSource(ProductEntity).ExecuteAffrows();

            if (affected >= 1) {
                base.OnDialogClosed(ButtonResult.OK);
            } else {
                _message.Error("编辑失败！");
            }
        });

        public DelegateCommand CancelCommand => new DelegateCommand(() => {
            base.OnDialogClosed(ButtonResult.Cancel);
        });


        protected override void OnViewLoaded() {

            var list = _mainDbOperate.GetModelList<CustomerEntity>(o => true);
            Customers.Clear();
            Customers.AddRange(list);

            if (_dialogParameters.ContainsKey("ProductEntity")) {

                ProductEntity = JsonConvert.DeserializeObject<ProductEntity>(_dialogParameters.GetValue<string>("ProductEntity"));
            }

        }
    }
}
